#!/usr/bin/env python
#################################################################################
#   Copyright (C) 2007 Daniel Kersten / Tim Kersten                             #
#   (See the file AUTHORS for details.)                                         #
#                                                                               #
#   Nexus-Ed is free software; you can redistribute it and/or modify            #
#   it under the terms of the GNU General Public License as published by        #
#   the Free Software Foundation; either version 2 of the License, or           #
#   (at your option) any later version.                                         #
#                                                                               #
#   Nexus-Ed is distributed in the hope that it will be useful,                 #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of              #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                #
#   GNU General Public License for more details.                                #
#                                                                               #
#   You should have received a copy of the GNU General Public License           #
#   along with Nexus-Ed; if not, write to the Free Software                     #
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA    #
#   or visit http://www.gnu.org/licenses/gpl.txt                                #
#                                                                               #
#################################################################################

try:
	import scintilla
except:
	print "Could not import PyGtkScintilla2"
	sys.exit(1)

from networkhandler import NetworkHandler

class Editor:
	"""
	Wrapper for the scintilla editor
	"""
	def __init__ (self, networkHandler, id=0):
		self.fileID = id
		self.networkHandler  = networkHandler
		
		# Set up the Scintilla widget.
		self.editor = scintilla.Scintilla()
		self.editor.SetMarginTypeN(4, 1)
		self.editor.SetMarginMaskN(4, 1)
		self.editor.SetMarginWidthN(4, 32)
		self.editor.connect("Modified", self.on_Command)
		self.editor.show()
	
	def get (self):
		return self.editor
		
	def on_Command (self, widget, position, flags, changetext, length, f, g, h, event):
		text = ''
		if changetext != None:
			text = changetext[:length] # Removes potential garbage.
		
		if widget == scintilla.SCEN_CHANGE:
			print "Change event"
	
		if flags & scintilla.SC_MOD_INSERTTEXT:
			# 'length' characters were inserted starting at 'position', the inserted text was 'text'
			print 'Inserted "' + text + '" at', position

		if flags & scintilla.SC_MOD_DELETETEXT:
			# 'length' characters were deleted starting at 'position', the deleted text was 'text'
			print 'Deleted "' + text + '" at', position
		
	def on_Edit (self):
		# Figure out what the changes were.
		self.networkHandler.notify(self.fileID, changes)
